package com.example.kotlin.ui

import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlin.HandGun
import com.example.kotlin.Magic
import com.example.kotlin.R
import com.example.kotlin.aspect.CheckNet
import com.example.myknife.ButterKnife
import com.example.myknife_annotations.BindView
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
    //val mainModel:MainModel by viewModels()
    @BindView(value = R.id.tv)
    lateinit var tv: TextView
    private var name: String? = null
    init {
        name="初始化"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ButterKnife.bind(this);
//       ViewUtil.inject(this)
       tv.text = name
        val m = Magic(HandGun(2)).apply {
            available
        }.apply {
            this.openBox()
        }
        val also: Magic<HandGun> = m.also {
            it.available
        }
        val run: Boolean = m.run {
            available
        }
        m.let {
            it.available
        }

        runBlocking {
            val a = async { doOne() }
            val b = async { doTwo() }

            Toast.makeText(
                this@MainActivity,
                StringBuilder().apply { append(a.await() + b.await()) }.toString(),
                Toast.LENGTH_SHORT
            ).show()
        }

        val coroutineScope: CoroutineScope =
            CoroutineScope(Job() + (Dispatchers.IO) + CoroutineName("cmj"))
        val job: Job = coroutineScope.launch() {
            println("job")
        }
        job.cancel()
        val job2: Job = coroutineScope.launch {

            withTimeout(30000) {
                withContext(Dispatchers.Main) {
                    Toast.makeText(this@MainActivity, "job2", Toast.LENGTH_SHORT).show()
                }
            }
        }
        //函数引用
        runSimulation("陈梦佳2022----------------", ::printConstructingCost, ::greetingFunction)

        runSimulationV2("罗店", 5)
        "I am from JiaDing".easyPrint()
        val listOf = listOf("zerbra", "monkey")
        listOf.map {
            "A baby $it"
        }.map {
            "$it,with the tail"
        }.toMutableSet().forEach {
            println(it)
        }
        listOf(listOf(1, 2, 3), listOf(4, 5, 6)).flatMap { it }.forEach {
            println(it)
        }
        val list = listOf(2, 3, 4, 5, 6, 7).filter { n ->
            (2 until n).map { n % it }.none { it == 0 }
        }
        println(list)
    }

    @CheckNet
    fun runStart(view: View) {
        println("开始")
    }

    private fun isLong(name: String) = name.length > 10

    private suspend fun doOne(): Int {
        delay(2000)
        return 2;
    }

    private suspend fun doTwo(): Int {
        delay(2000)
        return 12;
    }

    private suspend fun s(): String {
        var x = "2022cmj"
        withContext(Dispatchers.IO) {

            delay(2000)

        }
        return x
    }

    override fun onDestroy() {
        super.onDestroy()
        this.cancel()
    }

    private inline fun runSimulation(
        playerName: String,
        costPrinter: (Int) -> Unit,
        greetingFunction: (String, Int) -> String
    ) {
        val numBuilding = (1..3).shuffled().last()
        costPrinter(numBuilding)
        println(greetingFunction(playerName, numBuilding))
    }


    /**
     * 函数引用
     */
    private fun printConstructingCost(numBuildings: Int) {
        val cost = 500
        println("construction cost:${cost * numBuildings}")
    }

    private fun greetingFunction(playerName: String, num: Int): String {
        val currentYear = 2022
        println("Adding${num} houses")
        return "welcome to Shanghai,${playerName}!copyRight $currentYear"
    }

    /**
     * 函数作为返回值
     */
    private fun runSimulationV2(hospital: String, num: Int) {
        val configGreeting = configGreeting(hospital, num)
        println(configGreeting("桂芳"))
    }

    private fun configGreeting(hospital: String, num: Int): (String) -> String {
        var x = num
        return { playName: String ->

            val currentYear = 2022
            x += 1

            println("Adding $x $hospital")
            "welcome to Shanghai,$playName" +
                    "! (copyRight $currentYear)"


        }
    }

    /**
     * 泛型扩展函数
     */
    private fun <T> T.easyPrint() = println(this)

//    fun runStart(view: View) {
//
//    }
}
